package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.runningriding.entity.db.TbRunSuggest;
import com.quanyan.runningriding.entity.db.TbRunSuggestExample;
import com.quanyan.runningriding.entity.vo.run.req.RunSuggestVo;
import com.quanyan.runningriding.entity.vo.run.resp.RunSuggest;
import com.quanyan.runningriding.mapper.RunImproveSuggestMapper;
import com.quanyan.runningriding.mapper.TbRunSuggestMapper;
import com.quanyan.runningriding.service.RunSuggestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhiru.tan
 * @date 2016/9/21 0021
 */
@Service
public class RunSuggestServiceImpl implements RunSuggestService {
    @Autowired
    private TbRunSuggestMapper tbRunSuggestMapper;

    @Autowired
    private RunImproveSuggestMapper runImproveSuggestMapper;

    @Override
    public APIResponse<?> suggest(RunSuggestVo runSuggestVo) {
        TbRunSuggestExample tbRunSuggestExample = new TbRunSuggestExample();
        tbRunSuggestExample.createCriteria().andUserIdEqualTo(runSuggestVo.getUser_id());
        List<TbRunSuggest> runSuggests = tbRunSuggestMapper.selectByExample(tbRunSuggestExample);
        /**
         if (runSuggests != null && runSuggests.size() >= 3) {
         return APIResponse.returnFail("您提交改进建议只能三次");
         }
         */
        List<String> imgUrls = runSuggestVo.getImgUrls();
        StringBuilder stringBuilder = new StringBuilder();

        if (imgUrls != null && imgUrls.size() > 0) {
            for (int i = 0; i < imgUrls.size(); i++) {
                if (i == imgUrls.size() - 1) {
                    stringBuilder.append(imgUrls.get(i));
                } else {
                    stringBuilder.append(imgUrls.get(i) + ";");
                }

            }
        }

        TbRunSuggest tbRunSuggest = new TbRunSuggest();
        tbRunSuggest.setContent(runSuggestVo.getContent());
        tbRunSuggest.setImgUrl(stringBuilder.toString());
        tbRunSuggest.setUserId(runSuggestVo.getUser_id());
        tbRunSuggest.setCreateTime(new Date());
        tbRunSuggest.setUpdateTime(new Date());
        int i = tbRunSuggestMapper.insertSelective(tbRunSuggest);
        if (i > 0)
            return APIResponse.returnSuccess("改进建议提交成功");
        else
            return APIResponse.returnFail("改进建议提交异常");
    }

    @Override
    public PageObj<List<RunSuggest>> suggestList(String userId, String nickname, String content, Date startTime, Date endTime, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pageStart", (pageNum - 1) * pageSize);
        paramMap.put("pageSize", pageSize);
        paramMap.put("userId", userId);
        paramMap.put("nickname", nickname);
        paramMap.put("content", content);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);

        List<RunSuggest> suggestVos = runImproveSuggestMapper.suggestList(paramMap);

        if (suggestVos != null && suggestVos.size() > 0) {
            List<String> imgUrls = new ArrayList<>();
            for (RunSuggest runSuggest : suggestVos ){
                String imgUrl = runSuggest.getImgUrl();
                if (!StringUtil.isBlankOrNull(imgUrl)) {
                    String[] imgUrlStrs = imgUrl.split(";");
                    for(String imgUrlStr : imgUrlStrs){
                        imgUrls.add(imgUrlStr);
                    }
                    runSuggest.setImgUrls(imgUrls);
                    imgUrls.clear();
                }
                runSuggest.setImgUrl(null);
            }
        }
        Integer count = runImproveSuggestMapper.suggestCount();
        PageObj pageObj = PageObj.create(count, pageNum, pageSize, suggestVos);
        return pageObj;
    }

}